package com.appspot.checkflames.servlet;

import static com.appspot.checkflames.constants.CheckFlamesConstants.AMPERSAND_SIGN;
import static com.appspot.checkflames.constants.CheckFlamesConstants.*;
import static com.appspot.checkflames.constants.CheckFlamesConstants.EQUAL_SIGN;
import static com.appspot.checkflames.constants.CheckFlamesConstants.QUESTION_MARK;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;



@SuppressWarnings("serial")
public class Authorization extends HttpServlet {
	
	private static final Logger LOGGER = Logger.getLogger(Authorization.class);
	
	private static String REDIRECT_URL;
	private static final String REDIRECT_URL_PARAM = "redirect_uri";
	
	private static final String FACEBOOK_REDIRECT_URL_PARAM = "facebook_redirect_uri";
	private static String FACEBOOK_REDIRECT_URL;
	
	private static String APPLICATION_ID;
	private static final String APPLICATION_ID_PARAM = "client_id";
		
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		StringBuilder builder = new StringBuilder(FACEBOOK_REDIRECT_URL);
		builder.append(QUESTION_MARK);
		builder.append(APPLICATION_ID_PARAM);
		builder.append(EQUAL_SIGN);
		builder.append(APPLICATION_ID);
		builder.append(AMPERSAND_SIGN);
		builder.append(REDIRECT_URL_PARAM);
		builder.append(EQUAL_SIGN);
		builder.append(REDIRECT_URL);
		builder.append(AMPERSAND_SIGN);
		builder.append(SCOPE_PARAM);
		builder.append(EQUAL_SIGN);
		builder.append(SCOPE);
		builder.append(AMPERSAND_SIGN);
		builder.append(DISPLAY_PARAM);
		builder.append(EQUAL_SIGN);
		builder.append(DISPLAY);
		LOGGER.info(builder.toString());
		resp.sendRedirect(builder.toString());
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		REDIRECT_URL = config.getInitParameter(REDIRECT_URL_PARAM);
		APPLICATION_ID = config.getInitParameter(APPLICATION_ID_PARAM);
		FACEBOOK_REDIRECT_URL = config.getInitParameter(FACEBOOK_REDIRECT_URL_PARAM);
	}
}
